package metamodel.example;

import antlr.RecognitionException;
import antlr.TokenStreamException;
import junit.framework.TestCase;
import metamodel.ClassNode;
import metamodel.TraversalUtils;
import metamodel.misc.GraphViz;

import java.io.IOException;
import java.io.OutputStreamWriter;

/**
 * User: fleipold
 * Date: Oct 19, 2010
 * Time: 10:13:12 PM
 */
public class ExampleTest extends TestCase {
    private ClassNode completeOrderTraversal;
    private ClassNode itemOnlyOrderTraversal;

    public ExampleTest() throws TokenStreamException, RecognitionException {
        completeOrderTraversal = TraversalUtils.parse(
                "import (metamodel.example.*) " +
                        "Order( " +
                        "   customer: Customer(name, street, town, postCode), " +
                        "   items : collection(Item(amount, product))" +
                        ")"
        );


         itemOnlyOrderTraversal = TraversalUtils.parse(
                "import (metamodel.example.*) " +
                        "Order( " +
                        "   items : collection(Item(amount, product))" +
                        ")"
        );

    }

    public void testTraverse() throws TokenStreamException, IOException, RecognitionException {
        GraphViz.writeDot(TraversalUtils.createMetaModel(), completeOrderTraversal, true, "meta.dot");
        System.out.println(TraversalUtils.toString(TraversalUtils.createMetaModel(), completeOrderTraversal));

        Order order = new Order();
        Customer customer = new Customer();
        customer.setName("Felix Leipold");
        customer.setStreet("3 Hanover Gardens");
        customer.setTown("London");
        customer.setPostCode("SE11 5TL");
        order.setCustomer(customer);

        Item item1 = new Item();
        item1.setAmount(1);
        item1.setProduct("Mars Bar");
        order.getItems().add(item1);

        Item item2 = new Item();
        item2.setAmount(3);
        item2.setProduct("Apple");
        order.getItems().add(item2);


        Order clone = (Order) TraversalUtils.clone(completeOrderTraversal, order);
        assertTrue(order != clone);
        assertTrue(order.getItems().get(0) != clone.getItems().get(0));

        assertEquals("Felix Leipold", clone.getCustomer().getName());
        assertEquals("Mars Bar", clone.getItems().get(0).getProduct());

        System.out.println(TraversalUtils.toString(completeOrderTraversal, clone));

        Order itemOnlyClone = (Order) TraversalUtils.clone(itemOnlyOrderTraversal, order);

        
        System.out.println(TraversalUtils.toString(completeOrderTraversal, itemOnlyClone));
        

    }
}
